﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Wpf2.Models;
using WPFServices;

namespace Wpf2.Views
{
    /// <summary>
    /// Interaction logic for formEditBeforePrintOutInventory.xaml
    /// </summary>
    public partial class FormEditBeforePrintOutInventory 
    {
        bool _isMarcle;
        public USER User { get; set; }
        public Customer Cus { get; set; }
        public PaymentInfomation Payment { get; set; }
        public ObservableCollection<Products> ListProducts { get; set; }

        public FormEditBeforePrintOutInventory(IEnumerable<Products> listPro, USER user, Customer cus, PaymentInfomation payment, bool isMarcle)
        {
            try
            {
                InitializeComponent();
                _isMarcle = isMarcle;
                ListProducts = new ObservableCollection<Products>();
                foreach (var newPro in listPro.Select(pro => new Products
                                                                 {
                                                                          ProductIdAuto = pro.ProductIdAuto,
                                                                          ProductName = pro.ProductName,
                                                                          ProductId = pro.ProductId,
                                                                          ProductDescription = pro.ProductDescription,
                                                                          InventoryCount = pro.InventoryCount,
                                                                          Price = pro.Price,
                                                                          ProductHeight = pro.ProductHeight,
                                                                          ProductWidth = pro.ProductWidth,
                                                                          ProductM2 = pro.ProductM2,
                                                                          IsMarble = isMarcle,
                                                                          //IsCostPrice = pro.IsCostPrice,
                                                                          //CostPrice = pro.CostPrice,
                                                                          //SellPrice = pro.SellPrice,
                                                                          Unit = pro.Unit,
                                                                          catalog = pro.catalog,
                                                                          //TotalOutPayment = pro.TotalOutPayment,
                                                                          //TotalInPayment = pro.TotalInPayment
                                                                      }))
                {
                    ListProducts.Add(newPro);
                }

                //ListProducts.Add(new Products() { IsMarble = isMarcle });

                Payment = new PaymentInfomation { OldDebts = payment.OldDebts, PrePaid = payment.PrePaid, Remain = payment.Remain, SumOut = payment.SumOut, TotalPayment = payment.TotalPayment };
                User = user;
                Cus = cus;

                panelOfPaymentInfomation.DataContext = Payment;
                DataContext = ListProducts;
                if (!isMarcle)
                    hideColumnOfMarcle();
                    
                
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Ẩn 3 cột liên quan đế Đá Hoa Cương
        /// </summary>
        private void hideColumnOfMarcle()
        {
            dtgInventory.Columns[3].Visibility = System.Windows.Visibility.Hidden;
            dtgInventory.Columns[4].Visibility = System.Windows.Visibility.Hidden;
            dtgInventory.Columns[6].Visibility = System.Windows.Visibility.Hidden;
        }

        private void BtnPrintClick(object sender, RoutedEventArgs e)
        {
            try
            {
                IDictionary<string, string> param = new Dictionary<string, string>();
                param.Add("StoreName", txtStoreName.Text);
                param.Add("Address", txtAddress.Text);
                param.Add("Mobile", txtMobile.Text);
                param.Add("Phone", txtPhone.Text);
                
                var cryRep = new FormCryReOutInvetory(ListProducts, User, Cus, Payment, DateTime.Now, param, _isMarcle);
                    
                cryRep.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // xử lý khi ở ô cuối cùng người dùng bấm enter hoặc tag sẽ tạo hàng mới
        private void KeyDownExcuteAddNewRow(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter || e.Key == Key.Tab)
                ListProducts.Add(new Products() { IsMarble = _isMarcle });
        }

        private void IsAcceptNumberic(object sender, TextCompositionEventArgs e)
        {
            if (!char.IsDigit(e.Text, e.Text.Length - 1))
            {
                e.Handled = true;
            }
        }

        private void TxtInventoryCountTextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                CaculationPaymentForAllProduct();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void CaculationPaymentForAllProduct()
        {
            Payment.SumOut = 0;
            foreach (var pro in ListProducts.Where(pro => !string.IsNullOrEmpty(pro.ProductId)))
                Payment.SumOut += pro.TotalOutPayment;
        }

        
        private void CaculationPayment(object sender, TextChangedEventArgs e)
        {
            CaculationPaymentForAllProduct();
        }
    }
}
